package com.authority.controller;

import com.authority.entry.AuthActionDO;
import com.authority.query.AuthActionQuery;
import com.authority.service.AuthActionService;
import com.google.common.base.Preconditions;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 功能管理
 * @author limeng.g  Date 2015/10/15
 */
@Controller
@RequestMapping(value = "/auth/actionManager")
public class ActionManager extends BaseManager {

	@Resource
	private AuthActionService authActionService;

	/**
	 * 功能列表查询
	 * @param name 名称
	 * @param code 编码
	 * @param sysId 所属系统
	 * @param index 页码
	 * @return
	 */
	@RequestMapping(value = "/listAction.json")
	@ResponseBody
	public Object doListAction(String name,String code, Long sysId,int index) {

		try {
			checkParams(sysId);
			
			AuthActionQuery query = new AuthActionQuery();
			query.setName(name);
			query.setCode(code);
			query.setSysId(sysId);
			query.setIndex(index);
			query.setIsDownload(true);
			
			return success(authActionService.listAuthAction(query));
		} catch (Exception e) {
			e.printStackTrace();
			return fail(e);
		}
	}

	/**
	 * 获取功能详情
	 * @param id 功能id
	 * @return
	 */
	@RequestMapping(value = "/getActionById.json")
	@ResponseBody
	public Object doGetActionById(Long id) {

		try {
			checkParams(id);
			
			return success(authActionService.getAuthActionById(id));
		} catch (Exception e) {
			return fail(e);
		}
	}

	/**
	 * 添加功能
	 * @param name 名称
	 * @param code 编码
	 * @param sysId 所属系统
	 * @return
	 */
	@RequestMapping(value = "/saveAction.json")
	@ResponseBody
	public Object doSaveAction(String name,  String code, Long sysId) {

		try {
			checkParams(name,code,sysId);

			AuthActionDO db = new AuthActionDO();
			db.setName(name);
			db.setCode(code);
			db.setSysId(sysId);
			db.setOperatorId(getUserId());

			return success(authActionService.saveAuthAction(db));
		} catch (Exception e) {
			e.printStackTrace();
			return fail(e);
		}
	}

	/**
	 * 更新功能
	 * @param id
	 * @param name
	 * @param code
	 * @param sysId
	 * @return
	 */
	@RequestMapping(value = "/updateAction.json")
	@ResponseBody
	public Object doUpdateAction(Long id, String name,String code,Long sysId) {

		try {
			checkParams(id,name,code,sysId);

			AuthActionDO db = authActionService.getAuthActionById(id);
			Preconditions.checkNotNull(db);

			db.setName(name);
			db.setCode(code);
			db.setSysId(sysId);
			db.setOperatorId(getUserId());

			return success(authActionService.updateAuthAction(db));
		} catch (Exception e) {
			e.printStackTrace();
			return fail(e);
		}
	}
	
	/**
	 * 获取某系统下的所有功能(id,name)
	 * @param sysId
	 * @return
	 */
	@RequestMapping(value = "/listActionIdName.json")
	@ResponseBody
	public Object doListActionIdName(Long sysId){
		
		try {
			checkParams(sysId);
			
			AuthActionQuery query = new AuthActionQuery();
			query.setSysId(sysId);
			query.setIsDownload(true);
			
			return success(authActionService.listAuthActionIdName(sysId));
		} catch (Exception e) {
			return fail(e);
		}
	}

}
